package com.xebest.web.controller.purch;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.xebest.api.util.OpertorLogUtil;
import com.xebest.common.base.controller.BASEMessage;
import com.xebest.common.base.controller.BaseController;
import com.xebest.common.base.controller.BaseResponse;
import com.xebest.common.enums.erp.purch.VenSupplierCheckEnum;
import com.xebest.common.enums.erp.purch.VenSupplierStatusEnum;
import com.xebest.common.enums.erp.sys.VenDelEnum;
import com.xebest.common.enums.logtype.ErpLogTypeEnum;
import com.xebest.common.util.RequestUtil;
import com.xebest.common.util.XeBaseUtil;
import com.xebest.config.ParamConfig;
import com.xebest.util.DateUtil;
import com.xebest.util.SysSetBeanUtils;
import com.xebest.util.seq.PurchSeqUtil;
import com.xebest.web.controller.order.XeVenOrderController;
import com.xebest.web.form.XeVenPurchProductForm;
import com.xebest.web.form.XeVenPurchSupAptitudeForm;
import com.xebest.web.form.XeVenPurchSupContractForm;
import com.xebest.web.form.XeVenPurchSupInfoForm;
import com.xebest.web.model.base.XePageInfo;
import com.xebest.web.service.XeVenPurchSupplierService;
import com.xebest.web.view.*;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 供货商相关
 *
 * @author 李书峰
 */
@RestController
@RequestMapping("supplier")
public class XeVenSupplierController extends BaseController {

    private static final Logger log = LoggerFactory.getLogger(XeVenOrderController.class);//日志记录类

    @Autowired
    private XeVenPurchSupplierService xeVenPurchSupplierService;//供货商相关业务操作
    @Autowired
    private ParamConfig paramConfig;//获取公共配置信息

    /**
     * 新增供货商
     *
     * @author 李书峰
     */
    @RequestMapping("insertSupplier")
    public BaseResponse insertSupplier(@RequestBody @Validated XeVenPurchSupInfoForm form, BindingResult bindingResult) {
        log.info("--------------新增供货商 start-------------------");
        // 1、验证参数
        boolean parError = true;
        if (null != form
                && null != form.getLinkmanList()
                && null != form.getBank()
                //&& null != form.getAptitudeList()
                && form.getLinkmanList().size() > 0
            //&& form.getAptitudeList().size() > 0
                ) {
            parError = false;
        }
        if (parError) {
            return sendAjaxError(BASEMessage.PARAM_ERROR);
        }
        if (bindingResult.hasErrors()) {
            return sendAjaxError(BASEMessage.PARAM_ERROR, bindingResult.getFieldError().getDefaultMessage());
        }
        if ((null != form.getCheckStatus() ||
                null != form.getSupplierStatus())
                && !form.getCheckStatus().equals(form.getSupplierStatus())) {
            return sendAjaxError("审核状态和供应商状态不匹配！");
        }
        // 2、拼装参数
        //获取用户登录信息
        ErpUserInfo userInfo = SysSetBeanUtils.getErpUserInfo(super.getErpLoginUserInfo());
        form.setUserCode(userInfo.getUserCode());
        form.setModifyUser(userInfo.getLoginName());
        form.setSupplierCode(PurchSeqUtil.seqUtils.createSupplierCode());
        String operateContent = "新增供货商，供货商编码为：" + form.getSupplierCode();
        try {
            // 3、输出结果
            int result = xeVenPurchSupplierService.insertSupplier(form);
            log.info("新增供货商结果：", result);
            if (result > 0) {
                insertMqLog(ErpLogTypeEnum.erpSupplierInsertSupplier.getValue(), form.getSupplierCode(), operateContent);
                return sendAjaxOK();
            } else {
                return sendAjaxError(BASEMessage.ERROR);
            }
        } catch (Exception e) {
            log.error("新增供货商 insertSupplier()异常：{}", e);
            return sendAjaxError("新增供货商异常");
        } finally {
            log.info("--------------新增供货商 end-------------------");
        }
    }
    /**
     * 编辑供货商
     *
     * @author 李书峰
     */
    @RequestMapping("updateSupplier")
    public BaseResponse updateSupplier(@RequestBody XeVenPurchSupInfoForm form) {
        log.info("--------------更新供货商 start-------------------");
        // 1、验证参数
        boolean parError = true;
        if (null != form
                && StringUtils.isNotBlank(form.getSupplierCode())
                && null != form.getVersion()
                && null != form.getCheckStatus()
                && null != form.getSupplierStatus()
                && null != form.getAptitudeList()
                && form.getAptitudeList().size() > 0
                && (VenSupplierCheckEnum.FINANCE_REJECT.getValue().equals(form.getCheckStatus())//被商品驳回后可保存
                || VenSupplierCheckEnum.PRODUCT_REJECT.getValue().equals(form.getCheckStatus())//被财务驳回后可保存
                || VenSupplierCheckEnum.WAIT_SUBMIT.getValue().equals(form.getCheckStatus())
                || VenSupplierCheckEnum.PRODUCT_CHECK.getValue().equals(form.getCheckStatus())
        )//待提交
                && (VenSupplierStatusEnum.CHECKING.getValue().equals(form.getSupplierStatus())//待商品部审核
                || VenSupplierStatusEnum.WAIT_SUBMIT.getValue().equals(form.getSupplierStatus()))//待提交
                && null != form.getCheckStatusPre()
                && null != form.getSupplierStatusPre()
                && (VenSupplierCheckEnum.WAIT_SUBMIT.getValue().equals(form.getCheckStatusPre())
                || VenSupplierCheckEnum.FINANCE_REJECT.getValue().equals(form.getCheckStatusPre())
                || VenSupplierCheckEnum.PRODUCT_REJECT.getValue().equals(form.getCheckStatusPre())
                || VenSupplierCheckEnum.SIGNED.getValue().equals(form.getCheckStatusPre()))
                && (VenSupplierStatusEnum.WAIT_SUBMIT.getValue().equals(form.getSupplierStatusPre())
                || VenSupplierStatusEnum.CHECKING.getValue().equals(form.getSupplierStatusPre())
                || VenSupplierStatusEnum.FROZEN.getValue().equals(form.getSupplierStatusPre()))
                ) {
            parError = false;
        }
        if (parError) {
            return sendAjaxError(BASEMessage.PARAM_ERROR);
        }
        // 2、拼装参数
        //获取用户登录信息
        ErpUserInfo userInfo = SysSetBeanUtils.getErpUserInfo(super.getErpLoginUserInfo());
        form.setUserCode(userInfo.getUserCode());
        form.setModifyUser(userInfo.getLoginName());
        String operateContent = "编辑供货商，编码为：" + form.getSupplierCode() + "," + JSON.toJSONString(form);
        try {
            // 3、输出结果
            form.setUpdateAptitude(1);
            int result = xeVenPurchSupplierService.updateSupplier(form);
            log.info("更新供货商结果：", result);
            if (result > 0) {
                insertMqLog(ErpLogTypeEnum.erpSupplierUpdateSupplier.getValue(), form.getSupplierCode(), operateContent);
                return sendAjaxOK();
            } else {
                return sendAjaxOK("该供货商不存在，或已发生变更，请刷新页面重试！");
            }
        } catch (Exception e) {
            log.error("更新供货商 updateSupplier()异常：{}", e);
            return sendAjaxError("更新供货商异常");
        } finally {
            log.info("--------------更新供货商 end-------------------");
        }
    }

    /**
     * 删除供货商
     *
     * @author 李书峰
     */
    @RequestMapping("delSupplier")
    public BaseResponse delSupplier(@RequestBody XeVenPurchSupInfoForm form) {
        log.info("--------------删除供货商 start-------------------");
        // 1、验证参数
        boolean parError = true;
        if (null != form
                && null != form.getCheckStatusPre()
                && null != form.getSupplierStatusPre()
                && VenSupplierCheckEnum.WAIT_SUBMIT.getValue().equals(form.getCheckStatusPre())
                && VenSupplierStatusEnum.WAIT_SUBMIT.getValue().equals(form.getSupplierStatusPre())
                && StringUtils.isNotBlank(form.getSupplierCode())
                && null != form.getVersion()) {
            parError = false;
        }
        if (parError) {
            return sendAjaxError(BASEMessage.PARAM_ERROR);
        }
        // 2、拼装参数
        //获取用户登录信息
        ErpUserInfo userInfo = SysSetBeanUtils.getErpUserInfo(super.getErpLoginUserInfo());
        form.setUserCode(userInfo.getUserCode());
        form.setModifyUser(userInfo.getLoginName());
        form.setDelFlag(VenDelEnum.YES.getValue());
        String operateContent = "删除供货商，编码为：" + form.getSupplierCode();
        try {
            // 3、输出结果
            int result = xeVenPurchSupplierService.deleteSupplier(form);
            log.info("删除供货商结果：", result);
            if (result > 0) {
                insertMqLog(ErpLogTypeEnum.erpSupplierDelSupplier.getValue(), form.getSupplierCode(), operateContent);
                return sendAjaxOK();
            } else {
                return sendAjaxOK("该供货商不存在，或已发生变更，请刷新页面重试！");
            }
        } catch (Exception e) {
            log.error("删除供货商 delSupplier()异常：{}", e);
            return sendAjaxError("删除供货商异常");
        } finally {
            log.info("--------------删除供货商 end-------------------");
        }
    }

    /**
     * 商品部审核供货商
     *
     * @author 李书峰
     */
    @RequestMapping("purchCheck")
    public BaseResponse purchCheck(@RequestBody XeVenPurchSupInfoForm form) {
        log.info("--------------采购审核供货商 start-------------------");
        // 1、验证参数
        boolean parError = true;
        if (null != form
                && null != form.getCheckStatus()
                && null != form.getSupplierStatus()
                && VenSupplierCheckEnum.FINANCE_CHECK.getValue().equals(form.getCheckStatus())
                && VenSupplierStatusEnum.CHECKING.getValue().equals(form.getSupplierStatus())
                && null != form.getCheckStatusPre()
                && null != form.getSupplierStatusPre()
                && (VenSupplierCheckEnum.PRODUCT_CHECK.getValue().equals(form.getCheckStatusPre())
                || VenSupplierCheckEnum.FINANCE_REJECT.getValue().equals(form.getCheckStatusPre())
                || VenSupplierCheckEnum.PRODUCT_REJECT.getValue().equals(form.getCheckStatusPre()))
                && VenSupplierStatusEnum.CHECKING.getValue().equals(form.getSupplierStatusPre())
                && StringUtils.isNotBlank(form.getSupplierCode())
                && null != form.getVersion()) {
            parError = false;
            form.setPurchRejectReason(null);
        }
        if (parError) {
            return sendAjaxError(BASEMessage.PARAM_ERROR);
        }
        // 2、拼装参数
        //获取用户登录信息
        ErpUserInfo userInfo = SysSetBeanUtils.getErpUserInfo(super.getErpLoginUserInfo());
        form.setUserCode(userInfo.getUserCode());
        form.setModifyUser(userInfo.getLoginName());
        String operateContent = "商品部审核通过供货商，编码为：" + form.getSupplierCode();
        try {
            // 3、输出结果
            form.setUpdateAptitude(2);
            int result = xeVenPurchSupplierService.updateSupplier(form);
            log.info("采购审核供货商结果：", result);
            if (result > 0) {
                insertMqLog(ErpLogTypeEnum.erpSupplierPurchCheck.getValue(), form.getSupplierCode(), operateContent);
                return sendAjaxOK();
            } else {
                return sendAjaxOK("该供货商不存在，或已发生变更，请刷新页面重试！");
            }
        } catch (Exception e) {
            log.error("采购审核供货商 checkPurch()异常：{}", e);
            return sendAjaxError("采购审核供货商异常");
        } finally {
            log.info("--------------采购审核供货商 end-------------------");
        }
    }

    /**
     * 财务审核供货商
     *
     * @author 李书峰
     */
    @RequestMapping("financeCheck")
    public BaseResponse financeCheck(@RequestBody XeVenPurchSupInfoForm form) {
        log.info("--------------财务审核供货商 start-------------------");
        // 1、验证参数
        boolean parError = true;
        if (null != form
                && StringUtils.isNotBlank(form.getSupplierCode())
                && null != form.getCheckStatus()
                && null != form.getSupplierStatus()
                && VenSupplierCheckEnum.WAIT_SIGN.getValue().equals(form.getCheckStatus())
                && VenSupplierStatusEnum.CHECKING.getValue().equals(form.getSupplierStatus())
                && null != form.getCheckStatusPre()
                && null != form.getSupplierStatusPre()
                && VenSupplierCheckEnum.FINANCE_CHECK.getValue().equals(form.getCheckStatusPre())
                && VenSupplierStatusEnum.CHECKING.getValue().equals(form.getSupplierStatusPre())
                && null != form.getVersion()) {
            parError = false;
            form.setFinanceRejectReason(null);
        }
        if (parError) {
            return sendAjaxError(BASEMessage.PARAM_ERROR);
        }
        // 2、拼装参数
        //获取用户登录信息
        ErpUserInfo userInfo = SysSetBeanUtils.getErpUserInfo(super.getErpLoginUserInfo());
        form.setUserCode(userInfo.getUserCode());
        form.setModifyUser(userInfo.getLoginName());
        String operateContent = "财务部审核通过供货商，编码为：" + form.getSupplierCode();
        try {
            // 3、输出结果
            form.setUpdateAptitude(2);
            int result = xeVenPurchSupplierService.updateSupplier(form);
            log.info("财务审核供货商结果：", result);
            if (result > 0) {
                insertMqLog(ErpLogTypeEnum.erpSupplierFinanceCheck.getValue(), form.getSupplierCode(), operateContent);
                return sendAjaxOK();
            } else {
                return sendAjaxOK("该供货商不存在，或已发生变更，请刷新页面重试！");
            }
        } catch (Exception e) {
            log.error("财务审核供货商 financeCheck()异常：{}", e);
            return sendAjaxError("财务审核供货商异常");
        } finally {
            log.info("--------------财务审核供货商 end-------------------");
        }
    }

    /**
     * 签订供货商
     *
     * @author 李书峰
     */
    @RequestMapping("signSupplier")
    public BaseResponse signSupplier(@RequestBody XeVenPurchSupInfoForm form) {
        log.info("--------------签订供货商 start-------------------");
        // 1、验证参数
        boolean parError = true;
        if (null != form
                && StringUtils.isNotBlank(form.getSupplierCode())
                && null != form.getCheckStatus()
                && null != form.getSupplierStatus()
                && null != form.getCheckStatusPre()
                && null != form.getSupplierStatusPre()
                && VenSupplierCheckEnum.WAIT_SIGN.getValue().equals(form.getCheckStatusPre())
                && VenSupplierStatusEnum.CHECKING.getValue().equals(form.getSupplierStatusPre())
                && VenSupplierCheckEnum.SIGNED.getValue().equals(form.getCheckStatus())
                && VenSupplierStatusEnum.NORMAL.getValue().equals(form.getSupplierStatus())
                && null != form.getVersion()) {
            XeVenPurchSupContractForm contractForm = form.getContractForm();
            if (null != contractForm
                    && null != contractForm.getStartTimeStr()
                    && null != contractForm.getEndTimeStr()
                    && null != form.getFileList()
                    && form.getFileList().size() > 0) {
                List<XeVenPurchSupContractForm> contractList = new ArrayList<XeVenPurchSupContractForm>();
                for (int i = 0; i < form.getFileList().size(); i++) {
                    XeVenPurchSupContractForm contractFile = new XeVenPurchSupContractForm();
                    String fileUrl = form.getFileList().get(i);
                    contractFile.setContractUrl(fileUrl);
                    contractFile.setContractSort(i + 1);
                    contractFile.setStartTime(DateUtil.toDay(contractForm.getStartTimeStr()));
                    contractFile.setEndTime(DateUtil.toDay(contractForm.getEndTimeStr()));
                    contractList.add(contractFile);
                }
                form.setContractList(contractList);
                parError = false;
            }
        }
        if (parError) {
            return sendAjaxError(BASEMessage.PARAM_ERROR);
        }
        // 2、拼装参数
        //获取用户登录信息
        ErpUserInfo userInfo = SysSetBeanUtils.getErpUserInfo(super.getErpLoginUserInfo());
        form.setUserCode(userInfo.getUserCode());
        form.setModifyUser(userInfo.getLoginName());
        String operateContent = "签订供货商，编码为：" + form.getSupplierCode();
        try {
            // 3、输出结果
            form.setUpdateAptitude(2);
            int result = xeVenPurchSupplierService.updateSupplier(form);
            log.info("签订供货商结果：", result);
            if (result > 0) {
                insertMqLog(ErpLogTypeEnum.erpSupplierSignSupplier.getValue(), form.getSupplierCode(), operateContent);
                return sendAjaxOK();
            } else {
                return sendAjaxOK("该供货商不存在，或已发生变更，请刷新页面重试！");
            }
        } catch (Exception e) {
            log.error("签订供货商 signSupplier()异常：{}", e);
            return sendAjaxError("签订供货商异常");
        } finally {
            log.info("--------------签订供货商 end-------------------");
        }
    }

    /**
     * 冻结供货商
     *
     * @author 李书峰
     */
    @RequestMapping("frozenSupplier")
    public BaseResponse frozenSupplier(@RequestBody XeVenPurchSupInfoForm formSupInfo) {
        log.info("--------------冻结供货商 start-------------------");
        // 1、验证参数
        if (null == formSupInfo) {
            return sendAjaxError(BASEMessage.PARAM_ERROR);
        }
        List<XeVenPurchSupInfoForm> formList = formSupInfo.getSupInfoList();
        StringBuilder sbuilder = new StringBuilder("冻结供货商，编码为：");
        boolean parError = true;
        if (null != formList && formList.size() > 0) {
            for (XeVenPurchSupInfoForm form : formList) {
                if (null != form
                        && StringUtils.isNotBlank(form.getSupplierCode())
                        && null != form.getSupplierStatus()
                        && null != form.getCheckStatusPre()
                        && null != form.getSupplierStatusPre()
                        && VenSupplierCheckEnum.SIGNED.getValue().equals(form.getCheckStatusPre())
                        && VenSupplierStatusEnum.NORMAL.getValue().equals(form.getSupplierStatusPre())
                        && VenSupplierStatusEnum.FROZEN.getValue().equals(form.getSupplierStatus())
                        && null != form.getVersion()) {
                    parError = false;
                } else {
                    parError = true;
                    break;
                }
            }
        }

        if (parError) {
            return sendAjaxError(BASEMessage.PARAM_ERROR);
        }
        // 2、拼装参数
        //获取用户登录信息
        ErpUserInfo userInfo = SysSetBeanUtils.getErpUserInfo(super.getErpLoginUserInfo());
        String userCode = userInfo.getUserCode();
        String loginName = userInfo.getLoginName();
        for (XeVenPurchSupInfoForm form : formList) {
            form.setUserCode(userCode);
            form.setModifyUser(loginName);
            sbuilder.append(form.getSupplierCode()).append(",");
        }
        try {
            // 3、输出结果
            Map<String, Object> reult = xeVenPurchSupplierService.updateSupplierList(formList);

            int resultSize = Integer.valueOf(reult.get("successSize").toString());
            int failSize = Integer.valueOf(reult.get("failSize").toString());
            int totalSize = Integer.valueOf(reult.get("totalSize").toString());
            String msg = reult.get("msg").toString();
            if (resultSize > 0 && resultSize == totalSize) {
                insertMqLog(ErpLogTypeEnum.erpSupplierFrozenSupplier.getValue(), null, sbuilder.toString());
                return sendAjaxOK();
            } else if (resultSize > 0 && resultSize < totalSize) {
                msg = "冻结供货商成功" + resultSize + "条，失败" + failSize + "条，冻结" + msg;
                reult.put("msg", msg);
                sbuilder.append(msg);
                insertMqLog(ErpLogTypeEnum.erpSupplierFrozenSupplier.getValue(), null, sbuilder.toString());
                return sendAjaxOK(reult);
            } else {
                msg = "供货商不存在，或已发生变更，请刷新页面重试！";
                reult.put("msg", msg);
                return sendAjaxOK(reult);
            }
        } catch (Exception e) {
            log.error("冻结供货商 frozenSupplier()异常：{}", e);
            return sendAjaxError("冻结供货商异常");
        } finally {
            log.info("--------------冻结供货商 end-------------------");
        }
    }

    /**
     * 解冻供货商
     *
     * @author 李书峰
     */
    @RequestMapping("freeSupplier")
    public BaseResponse freeSupplier(@RequestBody XeVenPurchSupInfoForm formSupInfo) {
        log.info("--------------解冻供货商 start-------------------");
        // 1、验证参数
        if (null == formSupInfo) {
            return sendAjaxError(BASEMessage.PARAM_ERROR);
        }
        List<XeVenPurchSupInfoForm> formList = formSupInfo.getSupInfoList();
        StringBuilder sbuilder = new StringBuilder("解冻供货商，编码为：");
        boolean parError = true;
        if (null != formList && formList.size() > 0) {
            for (XeVenPurchSupInfoForm form : formList) {
                if (null != form
                        && StringUtils.isNotBlank(form.getSupplierCode())
                        && null != form.getSupplierStatus()
                        && null != form.getCheckStatusPre()
                        && null != form.getSupplierStatusPre()
                        && VenSupplierCheckEnum.SIGNED.getValue().equals(form.getCheckStatusPre())
                        && VenSupplierStatusEnum.FROZEN.getValue().equals(form.getSupplierStatusPre())
                        && VenSupplierStatusEnum.NORMAL.getValue().equals(form.getSupplierStatus())
                        && null != form.getVersion()) {
                    parError = false;
                } else {
                    parError = true;
                    break;
                }
            }
        }
        if (parError) {
            return sendAjaxError(BASEMessage.PARAM_ERROR);
        }
        // 2、拼装参数
        //获取用户登录信息
        ErpUserInfo userInfo = SysSetBeanUtils.getErpUserInfo(super.getErpLoginUserInfo());
        String userCode = userInfo.getUserCode();
        String loginName = userInfo.getLoginName();
        for (XeVenPurchSupInfoForm form : formList) {
            form.setUserCode(userCode);
            form.setModifyUser(loginName);
            sbuilder.append(form.getSupplierCode()).append(",");
        }
        try {
            // 3、输出结果
            Map<String, Object> reult = xeVenPurchSupplierService.updateSupplierList(formList);

            int resultSize = Integer.valueOf(reult.get("successSize").toString());
            int failSize = Integer.valueOf(reult.get("failSize").toString());
            int totalSize = Integer.valueOf(reult.get("totalSize").toString());
            String msg = reult.get("msg").toString();
            if (resultSize > 0 && resultSize == totalSize) {
                insertMqLog(ErpLogTypeEnum.erpSupplierFreeSupplier.getValue(), null, sbuilder.toString());
                return sendAjaxOK();
            } else if (resultSize > 0 && resultSize < totalSize) {
                msg = "解冻供货商成功" + resultSize + "条，失败" + failSize + "条，解冻" + msg;
                reult.put("msg", msg);
                sbuilder.append(msg);
                insertMqLog(ErpLogTypeEnum.erpSupplierFreeSupplier.getValue(), null, sbuilder.toString());
                return sendAjaxOK(reult);
            } else {
                msg = "供货商不存在，或已发生变更，请刷新页面重试！";
                reult.put("msg", msg);
                return sendAjaxOK(reult);
            }
        } catch (Exception e) {
            log.error("解冻供货商 frozenSupplier()异常：{}", e);
            return sendAjaxError("解冻供货商异常");
        } finally {
            log.info("--------------解冻供货商 end-------------------");
        }
    }

    /**
     * 查询供货商列表
     *
     * @author 李书峰
     */
    @RequestMapping("querySupplierList")
    public BaseResponse querySupplierList(@RequestBody XeVenPurchSupInfoForm form) {
        log.info("--------------查询供货商列表 start-------------------");
        // 1、验证参数
        if (null == form) {
            form = new XeVenPurchSupInfoForm();
        }
        // 2、拼装参数
        //获取用户登录信息
        XePageInfo<XeVenPurchSupInfoView> xePageInfo;
        PageHelper.startPage(form.getPageNum(), form.getPageSize(), true);
        //获取用户登录信息
        ErpUserInfo userInfo = SysSetBeanUtils.getErpUserInfo(super.getErpLoginUserInfo());
        form.setUserCode(userInfo.getUserCode());
        try {
            // 3、输出结果
            List<XeVenPurchSupInfoView> supplierList = xeVenPurchSupplierService.querySupplierList(form);
            log.info("查询供货商列表结果：", JSONArray.toJSON(supplierList).toString());
            xePageInfo = new XePageInfo<XeVenPurchSupInfoView>(supplierList);
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("supplierList", supplierList);
            map.put("xePageInfo", xePageInfo);
            return sendAjaxOK(map);
        } catch (Exception e) {
            log.error("查询供货商列表querysupplierList()异常：{}", e);
            return sendAjaxError("查询供货商列表异常");
        } finally {
            log.info("--------------查询供货商列表end-------------------");
        }
    }

    /**
     * 查询供货商审核列表
     *
     * @author 李书峰
     */
    @RequestMapping("querySupplierCheckList")
    public BaseResponse querySupplierCheckList(@RequestBody XeVenPurchSupInfoForm form) {
        log.info("--------------查询供货商列表 start-------------------");
        // 1、验证参数
        if (null == form) {
            form = new XeVenPurchSupInfoForm();
        }
        // 2、拼装参数
        //获取用户登录信息
        XePageInfo<XeVenPurchSupInfoView> xePageInfo;
        PageHelper.startPage(form.getPageNum(), form.getPageSize(), true);
        //获取用户登录信息
        ErpUserInfo userInfo = SysSetBeanUtils.getErpUserInfo(super.getErpLoginUserInfo());
        form.setUserCode(userInfo.getUserCode());
        form.setIsCheckList(1);
        form.setSupplierStatus(VenSupplierStatusEnum.CHECKING.getValue());
//		
//		if(null != form.getSearchType()){
//			if(new Integer(1).equals(form.getSearchType())){
//				form.setSupplierCode(form.getSearchText());
//			}else if(new Integer(2).equals(form.getSearchType())){
//				form.setSupplierName(form.getSearchText());
//			}
//		}
//		if(null != form.getSearchTimeType()){
//			if(new Integer(1).equals(form.getSearchTimeType())){
//				form.setSignDateStart(form.getSearchTimeStart());
//				form.setSignDateEnd(form.getSearchTimeEnd());
//			}else if(new Integer(2).equals(form.getSearchTimeType())){
//				form.setExpectedArrivalDateStart(form.getSearchTimeStart());
//				form.setExpectedArrivalDateEnd(form.getSearchTimeEnd());
//			}else if(new Integer(3).equals(form.getSearchTimeType())){
//				form.setCompleteDateStart(form.getSearchTimeStart());
//				form.setCompleteDateEnd(form.getSearchTimeEnd());
//			}
//		}
        try {
            // 3、输出结果
            List<XeVenPurchSupInfoView> supplierList = xeVenPurchSupplierService.querySupplierList(form);
            log.info("查询供货商列表结果：", JSONArray.toJSON(supplierList).toString());
            xePageInfo = new XePageInfo<XeVenPurchSupInfoView>(supplierList);
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("supplierList", supplierList);
            map.put("xePageInfo", xePageInfo);
            //拼接筛选字典
            JSONObject supCheckStatusDesc = new JSONObject();
            JSONArray supCheckStatus = new JSONArray();
            JSONObject supCheck = new JSONObject();
            supCheck.put("name", "请选择");
            supCheck.put("value", null);
            supCheckStatus.add(supCheck);
            JSONObject supCheck1 = new JSONObject();
            supCheck1.put("name", "待财务审核");
            supCheck1.put("value", VenSupplierCheckEnum.FINANCE_CHECK.getValue());
            supCheckStatus.add(supCheck1);
            JSONObject supCheck2 = new JSONObject();
            supCheck2.put("name", "财务驳回");
            supCheck2.put("value", VenSupplierCheckEnum.FINANCE_REJECT.getValue());
            supCheckStatus.add(supCheck2);
            supCheckStatusDesc.put("supCheckStatus", supCheckStatus);
            map.put("supCheckStatusDesc", supCheckStatusDesc);

            JSONObject supplierStatusDesc = new JSONObject();
            JSONArray supplierStatus = new JSONArray();
            JSONObject supStatus = new JSONObject();
            supStatus.put("name", "请选择");
            supStatus.put("value", null);
            supplierStatus.add(supStatus);
            JSONObject supStatus1 = new JSONObject();
            supStatus1.put("name", "审核中");
            supStatus1.put("value", VenSupplierStatusEnum.CHECKING.getValue());
            supplierStatus.add(supStatus1);
            supplierStatusDesc.put("supplierStatus", supplierStatus);
            map.put("supplierStatusDesc", supplierStatusDesc);
            return sendAjaxOK(map);
        } catch (Exception e) {
            log.error("查询供货商列表querysupplierList()异常：{}", e);
            return sendAjaxError("查询供货商列表异常");
        } finally {
            log.info("--------------查询供货商列表end-------------------");
        }
    }

    /**
     * 供货商详情
     *
     * @author 李书峰
     */
    @RequestMapping("querySupplierDetail")
    public BaseResponse querySupplierDetail(@RequestBody XeVenPurchSupInfoForm form) {
        log.info("--------------查询供货商详情 start-------------------");
        // 1、验证参数
        boolean parError = true;
        if (null != form
                && StringUtils.isNotBlank(form.getSupplierCode())
                && null != form.getVersion()) {
            parError = false;
        }
        if (parError) {
            return sendAjaxError(BASEMessage.PARAM_ERROR);
        }
        // 2、拼装参数
        //获取用户登录信息
        ErpUserInfo userInfo = SysSetBeanUtils.getErpUserInfo(super.getErpLoginUserInfo());
        form.setUserCode(userInfo.getUserCode());
        try {
            // 3、输出结果
            //form.setUserCode("SH170713518063");
            XeVenPurchSupInfoView supInfo = xeVenPurchSupplierService.querySupplierDetail(form);
            log.info("查询供货商详情结果：", JSONObject.toJSON(supInfo).toString());
            Map<String, Object> map = new HashMap<String, Object>();
            String picServer = paramConfig.getImgUrl();//图片服务器地址
            map.put("picServer", picServer);
            map.put("supInfo", supInfo);
            return sendAjaxOK(map);
        } catch (Exception e) {
            log.error("查询供货商详情querySupplierDetail()异常：{}", e);
            return sendAjaxError("查询供货商详情异常");
        } finally {
            log.info("--------------查询供货商详情end-------------------");
        }
    }

    /**
     * 查询历史签订
     *
     * @author 李书峰
     */
    @RequestMapping("queryContractList")
    public BaseResponse queryContractList(@RequestBody XeVenPurchSupInfoForm form) {
        log.info("--------------查询签订历史列表 start-------------------");
        // 1、验证参数
        // 2、拼装参数
        boolean parError = true;
        if (null != form
                && StringUtils.isNotBlank(form.getSupplierCode())) {
            parError = false;
        }
        if (parError) {
            return sendAjaxError(BASEMessage.PARAM_ERROR);
        }
        XePageInfo<XeVenPurchSupContractView> xePageInfo;
        PageHelper.startPage(form.getPageNum(), form.getPageSize(), true);
        //获取用户登录信息
        ErpUserInfo userInfo = SysSetBeanUtils.getErpUserInfo(super.getErpLoginUserInfo());
//		form.setUserCode("SH170713518063");
        form.setUserCode(userInfo.getUserCode());
        try {
            // 3、输出结果
            List<XeVenPurchSupContractView> contracList = xeVenPurchSupplierService.queryContractList(form);
            log.info("查询签订历史列表结果：", JSONArray.toJSON(contracList).toString());
            xePageInfo = new XePageInfo<XeVenPurchSupContractView>(contracList);
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("contracList", contracList);
            map.put("xePageInfo", xePageInfo);
            return sendAjaxOK(map);
        } catch (Exception e) {
            log.error("查询签订历史列表queryContractList()异常：{}", e);
            return sendAjaxError("查询签订历史列表异常");
        } finally {
            log.info("--------------查询签订历史列表end-------------------");
        }
    }

    /**
     * 查询合同文件
     *
     * @author 李书峰
     */
    @RequestMapping("queryContractFileList")
    public BaseResponse queryContractFileList(@RequestBody XeVenPurchSupInfoForm form) {
        log.info("--------------查询合同文件列表 start-------------------");
        // 1、验证参数
        // 2、拼装参数
        boolean parError = true;
        if (null != form
                && StringUtils.isNotBlank(form.getSupplierCode())) {
            parError = false;
        }
        if (parError) {
            return sendAjaxError(BASEMessage.PARAM_ERROR);
        }
        //获取用户登录信息
        ErpUserInfo userInfo = SysSetBeanUtils.getErpUserInfo(super.getErpLoginUserInfo());
        form.setUserCode(userInfo.getUserCode());
        try {
            // 3、输出结果
            List<String> contracList = xeVenPurchSupplierService.queryContractFileList(form);
            log.info("查询合同文件列表结果：", JSONArray.toJSON(contracList).toString());
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("contracList", contracList);
            return sendAjaxOK(map);
        } catch (Exception e) {
            log.error("查询合同文件列表queryContractFileList()异常：{}", e);
            return sendAjaxError("查询合同文件列表列表异常");
        } finally {
            log.info("--------------查询合同文件列表列表end-------------------");
        }
    }

    /**
     * 查询供货商商品购买记录列表
     *
     * @author 李书峰
     */
    @RequestMapping("queryProductList")
    public BaseResponse queryProductList(@RequestBody XeVenPurchProductForm form) {
        log.info("--------------查询供货商商品购买记录列表 start-------------------");
        // 1、验证参数
        // 2、拼装参数
        if (null == form) {
            form = new XeVenPurchProductForm();
        }
        XePageInfo<Map<String, Object>> xePageInfo;
        PageHelper.startPage(form.getPageNum(), form.getPageSize(), true);
        //获取用户登录信息
        ErpUserInfo userInfo = SysSetBeanUtils.getErpUserInfo(super.getErpLoginUserInfo());
        form.setUserCode(userInfo.getUserCode());
        if (null != form.getSearchType()
                && StringUtils.isNotBlank(form.getSearchText())) {
            if (new Integer(1).equals(form.getSearchType())) {
                form.setProName(form.getSearchText());
            } else if (new Integer(2).equals(form.getSearchType())) {
                form.setProNo(form.getSearchText());
            } else if (new Integer(3).equals(form.getSearchType())) {
                form.setSupplierName(form.getSearchText());
            }
        }

        try {
            // 3、输出结果
            List<Map<String, Object>> productList = xeVenPurchSupplierService.queryProductList(form);
            log.info("查询供货商商品购买记录列表结果：", JSONArray.toJSON(productList).toString());
            xePageInfo = new XePageInfo<Map<String, Object>>(productList);
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("productList", productList);
            map.put("xePageInfo", xePageInfo);
            return sendAjaxOK(map);
        } catch (Exception e) {
            log.error("查询供货商商品购买记录列表queryProductList()异常：{}", e);
            return sendAjaxError("查询供货商商品购买记录列表异常");
        } finally {
            log.info("--------------查询供货商商品购买记录列表end-------------------");
        }
    }

    /**
     * 商品部驳回审核供货商
     *
     * @author 李书峰
     */
    @RequestMapping("purchReject")
    public BaseResponse purchReject(@RequestBody XeVenPurchSupInfoForm form) {
        log.info("--------------采购审核供货商 start-------------------");
        // 1、验证参数
        boolean parError = true;
        if (null != form
                && null != form.getCheckStatus()
                && null != form.getSupplierStatus()
                && VenSupplierCheckEnum.PRODUCT_REJECT.getValue().equals(form.getCheckStatus())
                && VenSupplierStatusEnum.CHECKING.getValue().equals(form.getSupplierStatus())
                && null != form.getCheckStatusPre()
                && null != form.getSupplierStatusPre()
                && VenSupplierCheckEnum.PRODUCT_CHECK.getValue().equals(form.getCheckStatusPre())
                && VenSupplierStatusEnum.CHECKING.getValue().equals(form.getSupplierStatusPre())
                && StringUtils.isNotBlank(form.getSupplierCode())
                && StringUtils.isNotBlank(form.getPurchRejectReason())
                && null != form.getVersion()) {
            parError = false;
        }
        if (parError) {
            return sendAjaxError(BASEMessage.PARAM_ERROR);
        }
        // 2、拼装参数
        //获取用户登录信息
        ErpUserInfo userInfo = SysSetBeanUtils.getErpUserInfo(super.getErpLoginUserInfo());
        form.setUserCode(userInfo.getUserCode());
        form.setModifyUser(userInfo.getLoginName());
        String operateContent = "商品部审核驳回供货商，编码为" + form.getSupplierCode() + "，驳回原因：" + form.getPurchRejectReason();
        try {
            // 3、输出结果
            form.setUpdateAptitude(2);
            int result = xeVenPurchSupplierService.updateSupplier(form);
            log.info("采购审核供货商结果：", result);
            if (result > 0) {
                insertMqLog(ErpLogTypeEnum.erpSupplierPurchReject.getValue(), form.getSupplierCode(), operateContent);
                return sendAjaxOK();
            } else {
                return sendAjaxOK("供货商不存在，审核失败！");
            }
        } catch (Exception e) {
            log.error("采购审核供货商 purchReject()异常：{}", e);
            return sendAjaxError("采购审核供货商异常");
        } finally {
            log.info("--------------采购审核供货商 end-------------------");
        }
    }

    /**
     * 财务驳回供货商
     *
     * @author 李书峰
     */
    @RequestMapping("financeReject")
    public BaseResponse financeReject(@RequestBody XeVenPurchSupInfoForm form) {
        log.info("--------------财务审核供货商 start-------------------");
        // 1、验证参数
        boolean parError = true;
        if (null != form
                && StringUtils.isNotBlank(form.getSupplierCode())
                && StringUtils.isNotBlank(form.getFinanceRejectReason())
                && null != form.getCheckStatus()
                && null != form.getSupplierStatus()
                && VenSupplierCheckEnum.FINANCE_REJECT.getValue().equals(form.getCheckStatus())
                && VenSupplierStatusEnum.CHECKING.getValue().equals(form.getSupplierStatus())
                && null != form.getCheckStatusPre()
                && null != form.getSupplierStatusPre()
                && VenSupplierCheckEnum.FINANCE_CHECK.getValue().equals(form.getCheckStatusPre())
                && VenSupplierStatusEnum.CHECKING.getValue().equals(form.getSupplierStatusPre())
                && null != form.getVersion()) {
            parError = false;
        }
        if (parError) {
            return sendAjaxError(BASEMessage.PARAM_ERROR);
        }
        // 2、拼装参数
        //获取用户登录信息
        ErpUserInfo userInfo = SysSetBeanUtils.getErpUserInfo(super.getErpLoginUserInfo());
        form.setUserCode(userInfo.getUserCode());
        form.setModifyUser(userInfo.getLoginName());
        String operateContent = "财务部审核驳回供货商，编码为" + form.getSupplierCode() + "，驳回原因：" + form.getFinanceRejectReason();
        try {
            // 3、输出结果
            form.setUpdateAptitude(2);
            int result = xeVenPurchSupplierService.updateSupplier(form);
            log.info("财务审核供货商结果：", result);
            if (result > 0) {
                insertMqLog(ErpLogTypeEnum.erpSupplierFinanceReject.getValue(), form.getSupplierCode(), operateContent);
                return sendAjaxOK();
            } else {
                return sendAjaxOK("该供货商不存在，审核失败！");
            }
        } catch (Exception e) {
            log.error("财务驳回供货商 financeReject()异常：{}", e);
            return sendAjaxError("财务驳回供货商异常");
        } finally {
            log.info("--------------财务审核供货商 end-------------------");
        }
    }

    /**
     * 商户列表
     *
     * @author 李书峰
     */
    @RequestMapping("queryShopList")
    public BaseResponse queryShopList(@RequestBody XeVenPurchSupInfoForm form) {
        log.info("--------------查询商户列表start-------------------");
        // 1、验证参数
        if (null == form) {
            form = new XeVenPurchSupInfoForm();
        }
        // 2、拼装参数paramStr
        XePageInfo<Map<String, Object>> xePageInfo;
        PageHelper.startPage(form.getPageNum(), form.getPageSize(), true);
        //获取用户登录信息
        ErpUserInfo userInfo = SysSetBeanUtils.getErpUserInfo(super.getErpLoginUserInfo());
        form.setUserCode(userInfo.getUserCode());

//		Integer searchType = form.getSearchType() != null && StringUtils.isNotBlank(form.getSearchType().toString()) ? Integer.valueOf(form.getSearchType().toString()) : null ;
//		String searchText = 
        try {
            // 3、输出结果
            JSONObject shopObject = XeBaseUtil.baseUtil.vendorInfoListForErp(form.getSearchText(), form.getPageNum(), form.getPageSize());
            log.info("查询商户列表结果：", shopObject.toString());
            List<Map<String, Object>> shopList = (List<Map<String, Object>>) shopObject.get("venInfoList");
            Map<String, Object> map = new HashMap<String, Object>();
            if (null != shopList && shopList.size() > 0) {
                map.put("shopList", shopList);
            }
            xePageInfo = new XePageInfo<Map<String, Object>>(shopList);
            map.put("xePageInfo", xePageInfo);
            return sendAjaxOK(map);
        } catch (Exception e) {
            log.error("查询商户列表queryShopList()异常：{}", e);
            return sendAjaxError("查询商户列表异常");
        } finally {
            log.info("--------------查询商户列表end-------------------");
        }
    }

    /**
     * 商户详情
     *
     * @author 李书峰
     */
    @RequestMapping("queryShopDetail")
    public BaseResponse queryShopDetail(@RequestBody XeVenPurchSupInfoForm form) {
        log.info("--------------查询商户详情 start-------------------");
        // 1、验证参数
        boolean parError = true;
        if (null != form
                && StringUtils.isNotBlank(form.getUserCode())) {
            parError = false;
        }
        if (parError) {
            return sendAjaxError(BASEMessage.PARAM_ERROR);
        }
        // 2、拼装参数
        try {
            // 3、输出结果
            JSONObject shop = XeBaseUtil.baseUtil.vendorInfoForErp(form.getUserCode());
            log.info("查询商户详情结果：", shop.toString());
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("shop", shop);
            return sendAjaxOK(map);
        } catch (Exception e) {
            log.error("查询商户详情queryShopDetail()异常：{}", e);
            return sendAjaxError("查询商户详情异常");
        } finally {
            log.info("--------------查询商户详情end-------------------");
        }
    }

    /**
     * 商户详情列表
     *
     * @author 李书峰
     */
    @RequestMapping("queryShops")
    public BaseResponse queryShops(@RequestBody XeVenPurchSupInfoForm form) {
        log.info("--------------查询商户列表start-------------------");
        // 1、验证参数
        if (null == form) {
            form = new XeVenPurchSupInfoForm();
        }
        // 2、拼装参数paramStr
        XePageInfo<XeVenPurchSupInfoView> xePageInfo;
        PageHelper.startPage(form.getPageNum(), form.getPageSize(), true);
        //获取用户登录信息
        ErpUserInfo userInfo = SysSetBeanUtils.getErpUserInfo(super.getErpLoginUserInfo());
        form.setUserCode(userInfo.getUserCode());
        try {
            // 3、输出结果
            JSONObject retJson = XeBaseUtil.baseUtil.vendorInfoListForErp(form.getSearchText(), form.getPageNum(), form.getPageSize());
            log.info("查询商户列表结果：", retJson.toString());
            List<Map<String, Object>> shopList = (List<Map<String, Object>>) retJson.get("venInfoList");
            List<XeVenPurchSupInfoView> shops = new ArrayList<XeVenPurchSupInfoView>();
            Map<String, Object> map = new HashMap<String, Object>();
            if (null != shopList && shopList.size() > 0) {
                for (Map<String, Object> mm : shopList) {
                    String userCode = mm.get("userCode") != null && StringUtils.isNotBlank(mm.get("userCode").toString()) ? mm.get("userCode").toString() : null;
                    if (StringUtils.isNotBlank(userCode)) {
                        XeVenPurchSupInfoView shop = new XeVenPurchSupInfoView();
                        List<XeVenPurchSupLinkmanView> linkmanList = new ArrayList<XeVenPurchSupLinkmanView>();
                        XeVenPurchSupLinkmanView linkMan = new XeVenPurchSupLinkmanView();
                        JSONObject shopDetail = XeBaseUtil.baseUtil.vendorInfoForErp(userCode);
                        Integer provinceId = shopDetail.get("provinceId") != null && StringUtils.isNotBlank(shopDetail.get("provinceId").toString()) ? Integer.valueOf(shopDetail.get("provinceId").toString()) : null;
                        String provinceName = null != provinceId ? XeBaseUtil.baseUtil.getAreaName(provinceId) : null;
                        linkMan.setProvinceName(provinceName);
                        linkMan.setProvinceId(provinceId);
                        Integer cityId = shopDetail.get("cityId") != null && StringUtils.isNotBlank(shopDetail.get("cityId").toString()) ? Integer.valueOf(shopDetail.get("cityId").toString()) : null;
                        String cityName = null != cityId ? XeBaseUtil.baseUtil.getAreaName(cityId) : null;
                        linkMan.setCityName(cityName);
                        linkMan.setCityId(cityId);
                        linkMan.setStreet(shopDetail.get("venAddr") != null && StringUtils.isNotBlank(shopDetail.get("venAddr").toString()) ? shopDetail.get("venAddr").toString() : null);
                        linkMan.setLinkName(shopDetail.get("userName") != null && StringUtils.isNotBlank(shopDetail.get("userName").toString()) ? shopDetail.get("userName").toString() : null);
                        linkMan.setLinkMobile(shopDetail.get("userPhone") != null && StringUtils.isNotBlank(shopDetail.get("userPhone").toString()) ? shopDetail.get("userPhone").toString() : null);
                        linkMan.setFirstLinkMan(2);
                        linkmanList.add(linkMan);
                        shop.setLinkmanList(linkmanList);

                        List<XeVenPurchSupAptitudeView> aptitudeList = new ArrayList<XeVenPurchSupAptitudeView>();
                        JSONArray aptitudeArray = shopDetail.getJSONArray("apList");
                        for (int i = 0; i < aptitudeArray.size(); i++) {
                            JSONObject aptitudeObject = aptitudeArray.getJSONObject(i);
                            XeVenPurchSupAptitudeView aptitudeView = new XeVenPurchSupAptitudeView();
                            aptitudeView.setApFlag(aptitudeObject.get("apFlag") != null && StringUtils.isNotBlank(aptitudeObject.get("apFlag").toString()) ? Integer.valueOf(aptitudeObject.get("apFlag").toString()) : null);
                            aptitudeView.setApName(aptitudeObject.get("apName") != null && StringUtils.isNotBlank(aptitudeObject.get("apName").toString()) ? aptitudeObject.get("apName").toString() : null);
                            aptitudeView.setApUrl(aptitudeObject.get("apUrl") != null && StringUtils.isNotBlank(aptitudeObject.get("apUrl").toString()) ? aptitudeObject.get("apUrl").toString() : null);
                            aptitudeView.setIdcardBack(aptitudeObject.get("idcardBack") != null && StringUtils.isNotBlank(aptitudeObject.get("idcardBack").toString()) ? aptitudeObject.get("idcardBack").toString() : null);
                            aptitudeView.setApNum(aptitudeObject.get("apNum") != null && StringUtils.isNotBlank(aptitudeObject.get("apNum").toString()) ? aptitudeObject.get("apNum").toString() : null);
                            aptitudeList.add(aptitudeView);
                        }
                        shop.setAptitudeList(aptitudeList);

                        XeVenPurchSupBankView bank = new XeVenPurchSupBankView();
                        bank.setAccBank(shopDetail.get("accBank") != null && StringUtils.isNotBlank(shopDetail.get("accBank").toString()) ? shopDetail.get("accBank").toString() : null);
                        bank.setBankAccCard(shopDetail.get("bankAccCard") != null && StringUtils.isNotBlank(shopDetail.get("bankAccCard").toString()) ? shopDetail.get("bankAccCard").toString() : null);
                        bank.setBankAccName(shopDetail.get("bankAccName") != null && StringUtils.isNotBlank(shopDetail.get("bankAccName").toString()) ? shopDetail.get("bankAccName").toString() : null);
                        bank.setBankId(shopDetail.get("accBankCode") != null && StringUtils.isNotBlank(shopDetail.get("accBankCode").toString()) ? shopDetail.get("accBankCode").toString() : null);
                        shop.setBank(bank);

                        shop.setShopCode(userCode);
                        shop.setSupplierName(shopDetail.get("venName") != null && StringUtils.isNotBlank(shopDetail.get("venName").toString()) ? shopDetail.get("venName").toString() : null);
                        shops.add(shop);
                    }
                }
                map.put("shops", shops);
            }
            xePageInfo = new XePageInfo<XeVenPurchSupInfoView>(shops);
            int total = 0;
            int countPage = 0;
            if (null != retJson.get("countNum") && StringUtils.isNotBlank(retJson.get("countNum").toString())) {
                total = Integer.valueOf(retJson.get("countNum").toString());
                xePageInfo.setTotal(total);
            }
            if (null != retJson.get("countPage") && StringUtils.isNotBlank(retJson.get("countPage").toString())) {
                countPage = Integer.valueOf(retJson.get("countPage").toString());
                xePageInfo.setPages(countPage);
            }
            xePageInfo.setPageNum(form.getPageNum());
            xePageInfo.setPageSize(form.getPageSize());
            String picServer = paramConfig.getImgUrl();//图片服务器地址
            map.put("picServer", picServer);
            map.put("xePageInfo", xePageInfo);
            return sendAjaxOK(map);
        } catch (Exception e) {
            log.error("查询商户列表queryShopList()异常：{}", e);
            return sendAjaxError("查询商户列表异常");
        } finally {
            log.info("--------------查询商户列表end-------------------");
        }
    }

    /**
     * 采购单搜索供货商列表（code和供货商名称）
     *
     * @author 李书峰
     */
    @RequestMapping("searchSupplierList")
    public BaseResponse searchSupplierList(@RequestBody XeVenPurchSupInfoForm form) {
        log.info("--------------查询供货商列表 start-------------------");
        // 1、验证参数
        if (null == form) {
            form = new XeVenPurchSupInfoForm();
        }
        // 2、拼装参数
        //获取用户登录信息
        XePageInfo<Map<String, Object>> xePageInfo;
        PageHelper.startPage(form.getPageNum(), form.getPageSize(), true);
        //获取用户登录信息
        ErpUserInfo userInfo = SysSetBeanUtils.getErpUserInfo(super.getErpLoginUserInfo());
        form.setUserCode(userInfo.getUserCode());

        if (null != form.getSearchType()) {
            if (new Integer(1).equals(form.getSearchType())) {
                form.setSupplierCodeSearch(form.getSearchText());
            } else if (new Integer(2).equals(form.getSearchType())) {
                form.setSupplierName(form.getSearchText());
            }
        }
        form.setSearchText(null);
        try {
            // 3、输出结果
            List<Map<String, Object>> supplierList = xeVenPurchSupplierService.searchSupplierList(form);
//			for(Map<String,Object> mm:supplierList){
//				mm.put("purchType", 2);
//			}
            log.info("查询供货商列表结果：", JSONArray.toJSON(supplierList).toString());
            xePageInfo = new XePageInfo<Map<String, Object>>(supplierList);
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("supplierList", supplierList);
            map.put("xePageInfo", xePageInfo);
            return sendAjaxOK(map);
        } catch (Exception e) {
            log.error("查询供货商列表querysupplierList()异常：{}", e);
            return sendAjaxError("查询供货商列表异常");
        } finally {
            log.info("--------------查询供货商列表end-------------------");
        }
    }

    private void insertMqLog(String logType, String key, String operatorContent) {
        try {
            String ipAddress = RequestUtil.getClientIpAddress(request);
            if (StringUtils.isBlank(logType)) {
                logType = ErpLogTypeEnum.erpStorage.getValue();
            }
            ErpUserInfo userInfo = SysSetBeanUtils.getErpUserInfo(super.getErpLoginUserInfo());
            String userCode = userInfo.getUserCode();
            OpertorLogUtil.opertorLog.sendOperatorLogByParam(ipAddress, key, logType, userInfo.getUserName(), operatorContent, userCode);
        } catch (Exception e) {
            log.error("供货商MQ日志插入失败,logType={},operatorContent={}", logType, operatorContent);
        }
    }

    /**
     * 修改供货商资质数据
     *
     * @author 李书峰
     */
    @RequestMapping("updateSupplierAptitude")
    public BaseResponse updateSupplierAptitude(@RequestBody XeVenPurchSupInfoForm formFirst) {
        log.info("--------------更新供货商资质 start-------------------");
        // 1、验证参数
        boolean parError = true;
        if (null != formFirst
                && StringUtils.isNotBlank(formFirst.getSupplierCode())
                && null != formFirst.getAptitudeList()
                && formFirst.getAptitudeList().size() > 0
                && null != formFirst.getVersion()
                && null != formFirst.getCheckStatusPre()
                && null != formFirst.getSupplierStatusPre()
                && (VenSupplierCheckEnum.WAIT_SIGN.getValue().equals(formFirst.getCheckStatusPre())
                || VenSupplierCheckEnum.SIGNED.getValue().equals(formFirst.getCheckStatusPre()))
                && (VenSupplierStatusEnum.NORMAL.getValue().equals(formFirst.getSupplierStatusPre())
                || VenSupplierStatusEnum.CHECKING.getValue().equals(formFirst.getSupplierStatusPre()))
                ) {
            parError = false;
        }
        if (parError) {
            if (!(null != formFirst
                    && null != formFirst.getAptitudeList()
                    && formFirst.getAptitudeList().size() > 0)) {
                return sendAjaxError("请完善相关资质");
            } else if (!(null != formFirst
                    && StringUtils.isNotBlank(formFirst.getSupplierCode())
                    && null != formFirst.getVersion()
                    && null != formFirst.getCheckStatusPre()
                    && null != formFirst.getSupplierStatusPre()
                    && (VenSupplierCheckEnum.WAIT_SIGN.getValue().equals(formFirst.getCheckStatusPre())
                    || VenSupplierCheckEnum.SIGNED.getValue().equals(formFirst.getCheckStatusPre()))
                    && (VenSupplierStatusEnum.NORMAL.getValue().equals(formFirst.getSupplierStatusPre())
                    || VenSupplierStatusEnum.CHECKING.getValue().equals(formFirst.getSupplierStatusPre())))
                    ) {
                return sendAjaxError("当前供货商无法进行资质修改");
            } else {
                return sendAjaxError(BASEMessage.PARAM_ERROR);
            }
        }
        // 2、拼装参数
        XeVenPurchSupInfoForm form = new XeVenPurchSupInfoForm();
        form.setAptitudeList(formFirst.getAptitudeList());
        form.setSupplierCode(formFirst.getSupplierCode());
        form.setVersion(formFirst.getVersion());
        //获取用户登录信息
        ErpUserInfo userInfo = SysSetBeanUtils.getErpUserInfo(super.getErpLoginUserInfo());
        form.setUserCode(userInfo.getUserCode());
        form.setModifyUser(userInfo.getLoginName());
        String operateContent = "编辑供货商资质，编码为：" + form.getSupplierCode() + "," + JSON.toJSONString(form);
        try {
            // 3、输出结果
            form.setUpdateAptitude(1);
            int result = xeVenPurchSupplierService.updateSupplier(form);
            log.info("更新供货商资质结果：", result);
            if (result > 0) {
                insertMqLog(ErpLogTypeEnum.erpSupplierUpdateAptitude.getValue(), form.getSupplierCode(), operateContent);
                return sendAjaxOK();
            } else {
                return sendAjaxOK("该供货商不存在，或已发生变更，请刷新页面重试！");
            }
        } catch (Exception e) {
            log.error("更新供货商资质 updateSupplierAptitude()异常：{}", e);
            return sendAjaxError("更新供货商异常");
        } finally {
            log.info("--------------更新供货商资质 end-------------------");
        }
    }

}
  
